package client.behavior;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import client.object.CObject3DClient;

import com.intel.bluetooth.BlueCoveConfigProperties;

import server.object.*;

import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteJ;
import wiiremotej.event.WRIREvent;
import wiiremotej.event.WiiRemoteListener;

public class WiimoteManager {

	private static WiimoteManager instance;
	private Map<String, WiiRemote> wiimotes;
	
	public static WiimoteManager getInstance(){
		if(instance == null){
			instance = new WiimoteManager();
		}
		return instance;
	}
	
	private WiimoteManager(){
		System.setProperty(BlueCoveConfigProperties.PROPERTY_JSR_82_PSM_MINIMUM_OFF, "true");
		wiimotes = new HashMap<String, WiiRemote>();
	}
	
	public void linkWiimoteToCObject3d(WiiRemote wiimote, CObject3DClient object3d){
		WiiRemoteListener newListener = new WiimoteListener(wiimote, object3d);
		wiimote.addWiiRemoteListener(newListener);
		//TODO v�rifier s'il n'y a pas plus a faire
	}
	
	public WiiRemote getWiimote(String adresse){
		WiiRemote wiimote = null;
        try {
        	if(adresse==null){
        		adresse = "0021BD776EBE";
        	}
        	if(wiimotes.containsKey(adresse)){
        		return wiimotes.get(adresse);
        	}
        	else{
        		wiimote = WiiRemoteJ.connectToRemote (adresse) ;
        		wiimote.setIRSensorEnabled (true, WRIREvent.BASIC) ;
        		wiimote.setAccelerometerEnabled (true) ;
        		wiimotes.put(adresse, wiimote);
        	}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return wiimote;
	}
	
	
}
